package com.hongdee.base.maint.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.maint.dao.IMaintantTypeDao;
import com.hongdee.base.maint.entity.TBcMaintantType;
import com.hongdee.base.maint.service.IMaintantTypeService;
import com.hongdee.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/15.
 */
@Service
public class MaintantTypeServiceImpl implements IMaintantTypeService {
    @Autowired
    private IMaintantTypeDao iMaintantTypeDao;
    private String tabName = "T_BC_MAINTAIN_TYPE";
    private String entity ="TBcMaintantType";

    /**
     * @param uid  		  登录id
     * @param start       当前页面
     * @param limit       页面大小
     * @param maintPCode    维修大类编码
     * @return Map
     * @throws Exception
     * @desc 分页查询
     * @author 徐钦勇
     */
    @Override
    public Map page(String uid, String start, String limit, String maintPCode) throws Exception {
        String sql = "select * from " + tabName + " where 1=1 ";
        String oid = TokenUtil.getCompanyId(uid);
        List params = new ArrayList();
        if(!StringUtils.isEmpty(maintPCode)){
            sql+=" and (maint_p_code = ? or maint_p_name like ? ) ";
            params.add(maintPCode);
            params.add("%"+maintPCode+"%");
        }
        if(!StringUtils.isEmpty(oid)){
            sql += " and company_id = ? ";
            params.add(oid);
        }
        return this.iMaintantTypeDao.page(sql,params.toArray(),start,limit);
    }
    /**
     * @param id 主键
     * @return Map
     * @throws Exception
     * @desc   维修类别详细
     * @author 徐钦勇
     */
    @Override
    public Map info(String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录");
        }
        String sql = "select * from " + tabName + " where id = ? ";
        return this.iMaintantTypeDao.info(sql, new Object[]{id});
    }
    /**
     * @param id 主键
     * @param status 状态
     * @return Map
     * @throws Exception
     * @desc   启用停用
     * @author 徐钦勇
     */
    @Override
    public void update(String id, String status) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录");
        }
        String hql = "update " + entity + " set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.iMaintantTypeDao.update(hql, params);
    }
    /**
     * @param data 实体
     * @return TBcCar
     * @throws Exception
     * @desc   保存或修改
     * @author 徐钦勇
     */
    @Override
    public TBcMaintantType saveOrUpdate(TBcMaintantType data) throws Exception {

        if(data.getMaintRef().equals("0")){
            if(StringUtils.isEmpty(data.getMaintTypeName())){
                throw new Exception("维修类别名称不能为空！");
            }
        }
        else{
            if(StringUtils.isEmpty(data.getMaintProjectName())){
                throw new Exception("维修项目名称不能为空！");
            }
        }
        return (TBcMaintantType) this.iMaintantTypeDao.saveOrUpdate(data);
    }

}
